package controllers.admin;

import models.System.SQL.SQL;
import models.SystemModels.SystemUser;
import net.sf.json.JSONObject;
import play.Logger;
import play.cache.Cache;
import play.libs.Codec;
import play.libs.Images;
import play.mvc.Controller;

import java.util.List;


/**
 * Created by Administrator on 2018-06-25.
 */
public class Admin_index extends Controller {
     public static void Admin_index(){
         render();
     }
     public static void Admin_Logo(){
         String id = Codec.UUID();
         render(id);


     }
     public static void Admin_Logos(){
         JSONObject json = new JSONObject();
         try{
             String name = params.get("name");
             String pwd =params.get("pwd");
             String randomID = params.get("randomID");
             String id = params.get("id");
             Logger.info(name+pwd+randomID+id+ Cache.get(id).toString());
             id = Cache.get(id).toString();
             if (!randomID.equals(id)){
                 json.put("code",0);
                 renderJSON(json);
             }

             StringBuffer sb = new StringBuffer();
             sb.append(SQL.ADMINUSER);
             String password = models.System.Systems.password.pwd(pwd);
             String sql = String.format(" where username ='%s'and userpwd='%s'",name,password);
             sb.append(sql);
             Logger.info(sql);
             List<SystemUser> user = SystemUser.find(sb.toString()).fetch();
             session.put("AdminName",user.get(0).UserName);
             Logger.info(""+user.size());
             if (!user.isEmpty()){
                 json.put("code",1);
                 renderJSON(json);
             }else {
                 json.put("code",-1);
                 renderJSON(json);
             }
         }catch (Exception ex){
             ex.printStackTrace();
             json.put("code",-1);
             renderJSON(json);
         }
     }
     public static void Admin_Img(String randomID){
        Images.Captcha captcha = Images.captcha();
        String code = captcha.getText("#E4EAFD");
        Logger.info(code);
        Cache.set(randomID, code, "10mn");//缓存在服务端
        Logger.info(Cache.get(randomID).toString());
        renderBinary(captcha);
     }
     public static void Admin_Imgs(){
         String id = Codec.UUID();
         Logger.info(id);
         renderJSON(id);
     }


}
